//
//  ZDynamicVC.m
//  WeClubs
//
//  Created by Zed Link on 26/3/2017.
//  Copyright © 2017 Zed. All rights reserved.
//

#import "ZDynamicVC.h"
#import "ZClubCell.h"
#import "ZTodoVC.h"
#import "ZGroupChatViewController.h"
#import "ZMessageCenterController.h"

#define TOP_SPACE 16
#define HORIZONTAL_SPACE 12
#define ClubCellHeight 128

static NSString * const ClubCellReuseID = @"ZClubCellReuseID";

@interface ZDynamicVC () <UICollectionViewDelegate, UICollectionViewDataSource>

/** 组织列表 */
@property (weak, nonatomic) UICollectionView *clubsView;

/** 组织数据源 */
@property (strong, nonatomic) NSArray <ZClubModel *> *clubModels;

@end

@implementation ZDynamicVC

#pragma mark - About Navigation

- (void)zConfigNavigation
{
    [super zConfigNavigation];
    
    [self zClearNavBgColorAndSeparator:NO];
    [self.navBgView setBackgroundColor:NAVBAR_BG_COLOR];
    [self.navSeparatorLine setImage:[UIImage imageNamed:@"shadow_nav"]];
    
    self.navigationItem.title = @"我的组织";
    self.navigationItem.rightBarButtonItem = [self zNavItemWithImage:@"navItem_message_blue" shouldOriginal:YES andSelector:@selector(messageAction)];
}

- (void)messageAction
{

    ZMessageCenterController *messageVc = [[ZMessageCenterController alloc] init];
    messageVc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:messageVc animated:YES];
}

#pragma mark - View Life Cycle

- (void)zLoadSubview
{
    [super zLoadSubview];
    
    // ========== 组织列表 ==========
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.itemSize = CGSizeMake(self.view.width - HORIZONTAL_SPACE * 2, ClubCellHeight);
    layout.minimumLineSpacing = HORIZONTAL_SPACE;
    layout.minimumInteritemSpacing = HORIZONTAL_SPACE;
    
    UICollectionView *clubsView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    [self.view addSubview:clubsView];
    self.clubsView = clubsView;
    
    clubsView.delegate = self;
    clubsView.dataSource = self;
    
    [clubsView setBackgroundColor:GENERAL_VIEW_BG_COLOR];
    [clubsView registerNib:[UINib nibWithNibName:@"ZClubCell" bundle:nil] forCellWithReuseIdentifier:ClubCellReuseID];
    clubsView.contentInset = UIEdgeInsetsMake(TOP_SPACE, 0, TOP_SPACE, 0);
    clubsView.bounces = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self.clubsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view);
        make.top.mas_equalTo(64);
        make.bottom.mas_equalTo(self.view.mas_bottom).offset(-49);
    }];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.clubModels = @[];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    NSDictionary *dataPara = @{
                               @"user_id" : @([ZUserInfo sharedInstance].userId),
                               @"token" : [ZUserInfo sharedInstance].token,
                               @"size" : @(0),
                               };
    [HTTPRequest POST:kGetMyClubsURL parameter:dataPara success:^(id responseObject) {
        NSArray <NSDictionary *> *clubDict = responseObject[@"data"][@"club"];
        
        NSMutableArray <ZClubModel *> *clubModels = [NSMutableArray arrayWithCapacity:clubDict.count];
        for (NSDictionary *dict in clubDict) {
            ZClubModel *clubModel = [[ZClubModel alloc] initWithDictionary:dict];
            [clubModels addObject:clubModel];
        }
        self.clubModels = clubModels;
    } failure:^(NSError *error) {
        
    }];
}

#pragma mark - UICollectionViewDataSource & UICollectionViewDelegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.clubModels.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZClubModel *model = self.clubModels[indexPath.row];
    
    ZClubCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ClubCellReuseID forIndexPath:indexPath];
    cell.model = model;
    
    #warning TODO dynamic chat
    //group chat
    cell.didSelectedGroupChat = ^(ZClubModel * model) {
        ZGroupChatViewController *chatVc = [[ZGroupChatViewController alloc] init];
        chatVc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:chatVc animated:YES];
        
    };
    
    cell.didSelectedTodo = ^(ZClubModel *model)
    {
        ZTodoVC *todoVC = [[ZTodoVC alloc] init];
        todoVC.clubModel = model;
        
        todoVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:todoVC animated:YES];
    };
    
    cell.didSelectedActivity = ^(ZClubModel *model) {
        
    };
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    #warning TODO dynamic跳转到组织详情
    // Todo: 跳转到组织详情
    ZLog(@"跳转到组织详情: %@", indexPath);
}

#pragma mark - Getter & Setter

- (void)setClubModels:(NSArray<ZClubModel *> *)clubModels
{
    _clubModels = clubModels;
    
    [self.clubsView reloadData];
}

@end
